/**
 * created by 江心才子, 2020/3/18
 * Copyright (c) 2020, 270628297@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.wyl

import com.wyl.domain.Student
import java.io.File
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.*

/**
 * 项目名称：LearnJava-JDBC
 * 创建人：江心才子
 * 创建时间：2020-03-18 13:54
 * 内容描述：
 * 修改说明：
 */
object JdbcUtil {
    private val url: String
    private val user: String
    private val password: String
    private val driver: String

    init {
        Properties().apply {
//            val path = JdbcUtil::class.java.classLoader.getResource("jdbc.properties")!!.path
//            load(File(path).reader())
            load(JdbcUtil::class.java.classLoader.getResourceAsStream("jdbc.properties"))
            url = getProperty("url")
            user = getProperty("user")
            password = getProperty("password")
            driver = getProperty("driver")
        }
        Class.forName(driver)
    }

    fun executeQuery(sql: String, vararg params: Any, block: (resultSet: ResultSet) -> Unit) {
        DriverManager.getConnection(
            url,
            user,
            password
        ).use { conn ->
            conn.prepareStatement(sql).use { stm ->
                stm.setParams(params)
                stm.executeQuery().use {
                    block(it)
                }
            }
        }
    }
}

fun PreparedStatement.setParams(params: Array<out Any>) {
    params.forEachIndexed { index, item ->
        val indexOffset = index + 1
        when (item) {
            is String -> setString(indexOffset, item)
            else -> {
            }
        }
    }
}
